package com.mm.serivce.common.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mm.domain.common.DCommonCode;
import com.mm.domain.taxi.DCarInfo;
import com.mm.domain.taxi.DMiniUserInfo;
import com.mm.mapper.common.DCommonCodeMapper;
import com.mm.serivce.common.IDCommonCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 数据字典表 服务实现类
 * </p>
 *
 * @author JueDream
 * @since 2021-08-24
 */
@Service
public class DCommonCodeServiceImpl extends ServiceImpl<DCommonCodeMapper, DCommonCode> implements IDCommonCodeService {
    private final DCommonCodeMapper codeMapper;

    public DCommonCodeServiceImpl(DCommonCodeMapper codeMapper) {
        this.codeMapper = codeMapper;
    }

    @Override
    public Page<DCommonCode> queryCommonCodePage(Integer page, Integer limit, Integer cCode, String commonText) {
        Page<DCommonCode> commonCodePage = new Page<>(page, limit);
        LambdaQueryWrapper<DCommonCode> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(cCode)) {
            queryWrapper.eq(DCommonCode::getCode, cCode);
        }
        if (StrUtil.isNotBlank(commonText)) {
            queryWrapper.like(DCommonCode::getName, commonText);
        }
        queryWrapper.orderByAsc(DCommonCode::getCode);
        commonCodePage = codeMapper.selectPage(commonCodePage, queryWrapper);
        return commonCodePage;
    }

    @Override
    public List<DCommonCode> queryCommonCodeList(Integer cCode) {
        LambdaQueryWrapper<DCommonCode> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(cCode)) {
            queryWrapper.eq(DCommonCode::getCode, cCode);
        }
        List<DCommonCode> commonCodeList = codeMapper.selectList(queryWrapper);
        return commonCodeList;
    }

    @Override
    public List<DCommonCode> queryCommonCodeList(List<Integer> cCode) {
        LambdaQueryWrapper<DCommonCode> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(cCode)) {
            queryWrapper.in(DCommonCode::getCode, cCode);
        }
        List<DCommonCode> commonCodeList = codeMapper.selectList(queryWrapper);
        return commonCodeList;
    }

    @Override
    public DCommonCode queryCommonCodeOne(Long commonId) {
        DCommonCode commonCode = new DCommonCode();
        if (commonId > 0){
            commonCode = codeMapper.selectById(commonId);
        }
        return commonCode;
    }

    @Override
    public boolean removeCommonCode(Long commonId) {
        boolean flag = false;
        DCommonCode commonCode = codeMapper.selectById(commonId);
        if (Objects.nonNull(commonCode)) {
            commonCode.setDelFlg(1);
            Integer result = codeMapper.updateById(commonCode);
            if (result > 0) {
                flag = true;
            }
        }
        return flag;
    }

    @Override
    public DCommonCode getCommonCode(DCommonCode commonCode) {
        DCommonCode commonNewCode = new DCommonCode();
        LambdaQueryWrapper<DCommonCode> queryWrapper = new LambdaQueryWrapper();
        if (commonCode.getCode() > 0) {
            queryWrapper.eq(DCommonCode::getCode, commonCode.getCode());
        }
        if (StrUtil.isNotBlank(commonCode.getName())) {
            queryWrapper.eq(DCommonCode::getName, commonCode.getName());
        }
        /**
         * isEmptyOfEntity 深层实体判断属性
         * isEmptyOfNormal 查询条件为空(不包含entity)
         * isEmptyOfWhere 查询条件为空(包含entity)
         */
        if (queryWrapper.nonEmptyOfNormal()) {
            commonNewCode = codeMapper.selectOne(queryWrapper);
        }

        return commonNewCode;
    }

    @Override
    public DCommonCode getCommonCodeByCode(Integer cCode) {
        DCommonCode commonNewCode = new DCommonCode();
        LambdaQueryWrapper<DCommonCode> queryWrapper = new LambdaQueryWrapper();
        if (cCode > 0) {
            queryWrapper.eq(DCommonCode::getCode, cCode);
        }
        if (queryWrapper.nonEmptyOfNormal()) {
            commonNewCode = codeMapper.selectOne(queryWrapper);
        }
        return commonNewCode;
    }
}
